package crm.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import crm.dto.Page;
import crm.service.ProductService;
import crm.service.impl.ProductServiceImpl;
 
/**
 * Servlet implementation class ProductListServlet
 */
public class ProductListServlet extends HttpServlet { 
	private static final long serialVersionUID = 1L;

	private ProductService productService = new ProductServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	       doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String pageNO = request.getParameter("pageNO");
		String pageSize = request.getParameter("pageSize");
		
		/*
		 * pageNO = "1"; pageSize="2";
		 */
		
		System.out.println("pageNO:"+pageNO);
		System.out.println("pageSize:"+pageSize);
		
		try {
			
			
			
			Page productList = productService.getProductListPage(Integer.parseInt(pageSize),Integer.parseInt(pageNO));
			
			String json = JSON.toJSONString(productList,SerializerFeature.WriteDateUseDateFormat);
			
			response.getWriter().write(json);

			
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
		

		
	}

}
